package book;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    private int number;//书籍编号
    private String title;//书名
    private String author;//作者
    private String category;//种类
    private int publishYear;//出版年份
    private boolean isBorrowed;//是否被借出
    private int borrowCount;//借出次数
    private LocalDateTime shelfDate;//上架时间

    //提供构造方法
    public Book(String title, String author, String category, int publishYear, LocalDateTime shelfDate) {
        this.title = title;
        this.author = author;
        this.category = category;
        this.publishYear = publishYear;
        this.shelfDate = shelfDate;
        this.isBorrowed = false;//参数默认
        this.borrowCount = 0;//参数默认
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    public LocalDateTime getShelfDate() {
        return shelfDate;
    }

    public void setShelfDate(LocalDateTime shelfDate) {
        this.shelfDate = shelfDate;
    }

    @Override
    public String toString() {
        return "Book{" +
                "编号：" + number +
                ", 书名：'" + title + '\'' +
                ", 作者：'" + author + '\'' +
                ", 种类：'" + category + '\'' +
                ", 出版年份：" + publishYear +
                ", 是否被借出：" + isBorrowed +
                ", 借出次数：" + borrowCount +
                ", 上架时间：" + shelfDate +
                '}';
    }
    public void increaseBorrowCount() {
        this.borrowCount++;
    }
    public void decreaseBorrowCount() {
        this.borrowCount--;
    }

    //将每一本书的信息变成字符串，当要获取信息时，以逗号分割很简单
    public String tostring() {
        StringBuilder string = new StringBuilder();
        string.append(number).append(",");
        string.append(title).append(",");
        string.append(author).append(",");
        string.append(category).append(",");
        string.append(publishYear).append(",");
        string.append(isBorrowed).append(",");
        string.append(borrowCount).append(",");
        string.append(shelfDate != null?shelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME):"null");
        return string.toString();
    }

    @Override
    public int compareTo(Book o) {
        return o.getBorrowCount() - this.getBorrowCount();
    }
}
